import utils from '@/plugins/utils'
import qs from 'qs'

import { http, apiServer } from '@/plugins/http'
import { K_USER_TOKEN, K_TOKEN_EXPIRE, K_USER_WXOPENID, TENANT_ID, IS_PROD, TIMESTAMP } from '@/shared/consts'

const state = {
  user: null,
}

const mutations = {
  setUserInfo(state, {
    userName,
    token,
  }) {
    state.user = {
      userName,
      token,
    }

    uni.setStorageSync(K_USER_TOKEN, token)
    utils.pLog('set user info: ', state.user)
  },
  clearStorage(state) {
    uni.setStorageSync(K_USER_TOKEN, '')
    state.user = null
  },
}

const actions = {
  async getUserInfo({ commit, state }, conf) {
    await new Promise((resl, rej) => {
      if (conf) {
        const {userName, token} = conf
        commit('setUserInfo', {
          userName,
          token,
        })
      }

      resl()
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}